package com.kin.rich.action.userAction;

import java.util.Date;
import java.util.Map;

import com.kin.rich.action.BaseAction;
import com.kin.rich.dao.UserDAO;
import com.kin.rich.pojo.User;
import com.opensymphony.xwork2.ActionContext;

public class RegAction extends BaseAction {

	private String userName;
	private String userPwd;
	private String checkPwd;
	private String phone;
	private String email;
	private UserDAO dao = new UserDAO();
	
	@Override
	public String execute() throws Exception {
		try{
			User user = new User();
			user.setEmail(email);
			user.setPhone(phone);
			user.setRegDate(new Date());
			user.setUserName(userName);
			user.setUserPwd(userPwd.hashCode()+"");
			if(this.check()){
				dao.save(user);
				Map session = ActionContext.getContext().getSession();
				session.put("user", user);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "input";
		}
		return "success";
	}
	
	public boolean check(){
		if(userName==null||userName.length()<6||userName.length()>30){
			message = "用户名有误！";
			return false;
		}
		else if(dao.findByUserName(userName).size()>0){
			message = "该用户已存在！";
			return false;
		}
		else if(userPwd!=checkPwd){
			message = "密码有误！";
			return false;
		}
		else return true;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public void setCheckPwd(String checkPwd) {
		this.checkPwd = checkPwd;
	}

	
	
	
	
}
